<?php
namespace Admin\Controller;
require_once 'Application/Common/Lib/Validate/Validate.php';

use Common\Lib\Validate\Validate;
/**
 * 表单相关控制器
 * Class FormController
 * @package Admin\Controller
 */
class FormController extends AbstractController {

    public function index(){
    	/*
        $keyword = I('keyword');
        if(!empty($keyword)){
            $where['Name|CName'] = array('like','%'.$keyword.'%');
        }
        $where['IsDel'] = array('neq','1');
        $count = M('Form')->where($where)->count();
        $page = $this->page($count, 20);
        $fields = '*';
        $list = M('Form')->where($where)->field($fields)->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list',$list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
        */
       $this->assign('list',$this->show(0));
       $this->display();
    }
    
    public function add(){
    
        if(IS_POST){
  
            $data = $_POST;
			$result = M('Form')->add($data);
            if($result){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
            //检测字段
            /*
            $validateResult = Validate::UserName($data['Name'],0,30);
            if($validateResult){
                $validateResult = Validate::SupplyChinese($data['CName'],0,30);
                if($validateResult){
                    $result = M('Form')->add($data);
                    if($result){
                        $this->success('添加成功');
                    }else{
                        $this->error('添加失败');
                    }
                }
            }
            */
        }else{
        	$fid = I("fid");
        	$list = M('Form')->where(array("ID" => $fid))->find();
        	//var_dump($list);
        	$this->assign('PName',$list['CName']);
            $this->display();
        }
    }
    
    public function edit(){
        if(IS_POST){
			$data = $_POST;
			$result = M('Form')->save($data);
			if($result){
                $this->success('修改成功');
           	}else{
                $this->error('修改失败');
            }
        }else{
        	$ID = (int)I("ID");
        	$list = M('Form')->where(array("ID" => $ID))->find();
        	$this->assign('data',$list);
        	//TP树 
            $Authrule = D("Form")->where(array('ID'=>$ID))->find();
	        $result = D("Form")->order(array("ID" => "desc"))->select();
	        $tree = new \Think\Tree();
	        foreach ($result as $r) {
	            $r['selected'] = $r['ID'] == $Authrule['ParentID'] ? 'selected' : '';
	            $array[] = $r;
	        }
	        $str = "<option value='\$ID' \$selected>\$spacer \$CName</option>";
	        $tree->init($array);
	        $select_categorys = $tree->get_tree(0, $str);
	        $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }
    
    public function delete(){
    	$ID = (int)I('ID');
    	$show = $this->show($ID);
    	if(empty($show)){
			$result = M('Form')->where(array("ID"=>$ID))->save(array("IsDel"=>1));
			if($result){
	            $this->success('添加成功');
	        }else{
	            $this->error('添加失败');
	        }
    	}else{
			$this->error('请先删除子表单！');
		}
    }
    
    
	public function show($pid = -1){
		if($pid == -1){
			$in = I();
			$parentID = $in['ParentID'];
		}else{
			$parentID = $pid;
		}
		$sql = "SELECT *,(SELECT count(id) FROM Form WHERE ParentID = a.ID AND IsDel = 0) AS coutsonID FROM Form AS a WHERE ParentID = {$parentID} AND IsDel = 0";
		$res = M('Form')->query($sql);
		if($pid == -1){
			$this->ajaxReturn($res);
		}else{
			return $res;
		}
    }
    
    public function fromtree(){
    	$this->display();
    }
    
    public function fromtreeData(){
    	$jsonid = json_decode(file_get_contents("php://input"));
    	$id = $jsonid->id ? $jsonid->id : 0;
    	$sql = "SELECT CName AS name, ID AS id, ParentID AS pid, (SELECT ID FROM Form WHERE ParentID = a.ID AND IsDel = 0 LIMIT 1) AS Sonid FROM Form AS a WHERE ParentID = {$id} AND IsDel = 0";
		$res = M('Form')->query($sql);
		foreach($res AS $k => $v){
			if(!empty($v['Sonid'])){
				$res[$k]['isParent'] = "true";
				//$res[$k]['icon'] = __APP__."./img/diy/22.png";
			} 
		}
    	$this->ajaxReturn($res);
    }
    
    //表单控件绑定流程表
    public function setBPformRight(){
    	$idStr = I('ID');
    	$BPID = I('processID');
    	if(!empty($idStr)){
    		$where1["FormID"] = array("in",$idStr);
    		$res1 = M("formcontrol")->field('*')->where($where1)->select();
    		foreach($res1 AS $k => $v){
    			$data['BPID'] = $BPID;
    			$data['ControlID'] = $v['ID'];
    			$data['RightType'] = 1;
    			$res3 = M("bp_form_right")->field('ID')->where(array("ControlID"=>$data['ControlID'],"BPID"=>$data['BPID']))->find(); //查询是否已经绑定过
    			if(empty($res3)){
    				$res2 = M("bp_form_right")->data($data)->add();
    			}
    		}
    		if($res2)$this->success('添加成功');
    	}else{
    		$this->error('请传入表单ID');
    	}
    }

	//获得该流程绑定的表单
	public function getBPformList(){
		$processID = I('processID');
		if(!empty($processID)){
			//$res1 = M("bp_form_right")->field('ControlID')->where(array("BPID"=>$processID))->select();
			$sql = "SELECT * FROM form WHERE ID IN (SELECT distinct(FormID) FROM formcontrol WHERE ID IN((SELECT ControlID FROM bp_form_right where BPID = {$processID})))";
			$res = M()->query($sql);
			$this->ajaxReturn($res);
		}
	}
	
}